/**
 * fileName:  FruitCategoryBean.java
 * author:  Lei
 * Company:  Cloudland Studio
 * Createdate:  2014-5-31 上午11:13:09
 *
 * All rights Reserved, Designed By cloudland Copyright(C) 2010-2011
 *   
 * Modification  History:
 *     Date           Author        Version        
 * -------------------------------------------
 * 2014-5-31          Lei            1.0        
 *
 * Why & What is modified:
 * 1.0: 文件创建
 */
package org.cloudland.commercial.masterdata.handler.support;

import java.util.ArrayList;
import java.util.List;

import org.cloudland.commercial.masterdata.bean.CategoryParam;
import org.cloudland.commercial.masterdata.bean.FruitCategoryInfo;
import org.cloudland.commercial.masterdata.store.entity.CategoryEntity;
import org.cloudland.commercial.masterdata.store.entity.ParamValueRelEntity;
import org.cloudland.commercial.masterdata.store.read.ArgumentCostReader;
import org.cloudland.commercial.masterdata.store.read.ArgumentReader;
import org.cloudland.commercial.masterdata.store.read.FruitCategoryReader;
import org.cloudland.commercial.masterdata.store.read.ParamValueRelReader;
import org.cloudland.commercial.masterdata.store.write.FruitCategoryWriter;

/**
 * <pre>
 * 水果分类信息
 * </pre>
 * 
 * @ClassName FruitCategoryBean
 * @author Lei
 * @version %I%, %G%
 * @see
 * @since JDK1.6
 * 
 */
public class FruitCategoryBean implements FruitCategoryInfo {

	/**
	 * categoryEntity 分类信息
	 */
	private CategoryEntity categoryEntity;

	/**
	 * childCategoryEntity 子分类信息
	 */
	private List<FruitCategoryInfo> childCategoryInfo = new ArrayList<FruitCategoryInfo>(6);
	
	/**
	 * fruitCategoryWriter 写数据库操作，用于新增、修改、删除数据库操作
	 */
	private FruitCategoryWriter fruitCategoryWriter;

	/**
	 * fruitCategoryReader 读数据库操作，用于查询数据库操作
	 */
	private FruitCategoryReader fruitCategoryReader;
	
	/**
	 * paramValueRelReader 分类引用参数关系读取接口
	 */
	private ParamValueRelReader paramValueRelReader;
	
	 /**
	 * argumentReader 参数信息读取接口
	 */
	private ArgumentReader argumentReader;
	
	 /**
	 * costReader 值信息读取接口
	 */
	private ArgumentCostReader costReader;

	/**
	 * <pre>
	 * TODO 请充分描述该构造函数的作用，及其参数的作用。
	 * </pre>
	 * 
	 */
	public FruitCategoryBean(CategoryEntity categoryEntity, FruitCategoryWriter fruitCategoryWriter, FruitCategoryReader fruitCategoryReader,
			ParamValueRelReader paramValueRelReader, ArgumentReader argumentReader, ArgumentCostReader costReader) {
		this.categoryEntity = categoryEntity;
		this.fruitCategoryWriter = fruitCategoryWriter;
		this.fruitCategoryReader = fruitCategoryReader;
		this.paramValueRelReader = paramValueRelReader;
		this.argumentReader = argumentReader;
		this.costReader = costReader;
	}

	/**
	 * <pre>
	 * TODO 请充分描述该构造函数的作用，及其参数的作用。
	 * </pre>
	 * 
	 * @param fruitCategoryWriter
	 * @param fruitCategoryReader
	 */
	public FruitCategoryBean(FruitCategoryWriter fruitCategoryWriter, FruitCategoryReader fruitCategoryReader,
			ParamValueRelReader paramValueRelReader, ArgumentReader argumentReader, ArgumentCostReader costReader) {
		this(new CategoryEntity(), fruitCategoryWriter, fruitCategoryReader, paramValueRelReader, argumentReader, costReader);
	}

	/**
	 * @return
	 */
	public String getId() {
		return categoryEntity.getId();
	}

	/**
	 * @return
	 */
	public String getName() {
		return categoryEntity.getName();
	}

	/**
	 * @param categoryName
	 */
	public void setName(String categoryName) {
		categoryEntity.setName(categoryName);
	}

	/**
	 * @return
	 */
	public Integer getSort() {
		return categoryEntity.getSort();
	}

	/**
	 * @param sort
	 */
	public void setSort(Integer sort) {
		categoryEntity.setSort(sort);
	}

	public void setParentId(String parentId) {
		categoryEntity.setParentId(parentId);
	}
	
	/**
	 * @return
	 */
	public CategoryParam[] getParam() {
		
		List<String> filterIds = new ArrayList<String>(6);
		filterIds.add(categoryEntity.getId());
		
		String id = categoryEntity.getParentId();
		CategoryEntity categoryTemp = null;
		while (null != id && id.length() > 0) {
			filterIds.add(id);
			
			categoryTemp = fruitCategoryReader.findCategoryById(id);
			id = categoryTemp.getParentId();
		}
		
		List<CategoryParamBean> paramBeans = new ArrayList<CategoryParamBean>(10);
		CategoryParamBean bean = null;
		boolean isExtend = false;
		for (String cateId : filterIds) {
			if (!cateId.equals(categoryEntity.getId())) {
				isExtend = true;
			}
			
			for (ParamValueRelEntity relEntity : paramValueRelReader.findParamValueRelByCategoryId(cateId)) {
				bean = new CategoryParamBean(relEntity, argumentReader, costReader);
				bean.setExtend(isExtend);
				paramBeans.add(bean);
			}
		}
		
		return paramBeans.toArray(new CategoryParamBean[paramBeans.size()]);
	}
	
	/**
	 * @return
	 */
	public FruitCategoryInfo[] getChilds() {

		List<CategoryEntity> categoryList = fruitCategoryReader	.findChildCategoryById(getId());
		if (null == categoryList || categoryList.size() <= 0) {
			return new FruitCategoryInfo[0];
		}

		if (categoryList.size() > 0) {
			childCategoryInfo = new ArrayList<FruitCategoryInfo>(categoryList.size());
		}

		for (CategoryEntity entity : categoryList) {
			childCategoryInfo.add(new FruitCategoryBean(entity, this.fruitCategoryWriter, this.fruitCategoryReader, this.paramValueRelReader, this.argumentReader, this.costReader));
		}

		return childCategoryInfo.toArray(new FruitCategoryInfo[childCategoryInfo.size()]);
	}

	/**
	 * @param categoryInfo
	 */
	public void addChild(FruitCategoryInfo categoryInfo) {
		childCategoryInfo.add(categoryInfo);
	}

	public void submit() {

		if (null != categoryEntity.getId() && categoryEntity.getId().length() > 0) {
			this.fruitCategoryWriter.update(categoryEntity);
		} else {
			this.fruitCategoryWriter.save(categoryEntity);
		}

		// 子分类不为空, 保存子分类
		for (FruitCategoryInfo child : childCategoryInfo) {
			((FruitCategoryBean) child).setParentId(this.getId());
			child.submit();
		}

	}
	
	/**
	 * @return
	 */
	@Override
	public String toString() {
		return String.valueOf(categoryEntity);
	}

}
